package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.HealthClock;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.service.RoleService;
import com.example.demo.vo.HealthClockVo;
import com.example.demo.service.HealthClockService;
import com.example.demo.vo.DataView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Coding路人王
 * @date 2022/5/26 20:46
 */
@Controller
public class HeathClockController {

    @Autowired
    private HealthClockService healthClockService;
    @Autowired
    private RoleService roleService;

    // 跳转面
    @RequestMapping("/toHealthClock")
    public String toHealthClock(){
        return "admin/healthclock";
    }


    /**
     * 查询所有打卡记录 带有模糊查询条件 带有分页
     * @param healthClockVo
     * @return
     */
    @RequestMapping("/listHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockVo healthClockVo){
        // 查询所有带有模糊查询条件 带有分页
        IPage<HealthClock> page = new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());
        QueryWrapper<HealthClock> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(healthClockVo.getUsername() != null,"username",healthClockVo.getUsername());
        queryWrapper.eq(healthClockVo.getPhone() != null,"phone",healthClockVo.getPhone());
        healthClockService.page(page,queryWrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加或者修改健康打卡记录数据
     */
    @RequestMapping("/addHealthClock")
    @ResponseBody
    public DataView addHealthClock(HealthClock healthClock){
        boolean b = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功！");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加失败！");
        return dataView;
    }

    @RequestMapping("/updateHealthClock")
    @ResponseBody
    public DataView updateHealthClock(HealthClock healthClock, HttpSession session){
        //根据角色判断是否能编辑
        // 1.取到
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<Integer> role = roleService.queryUserRoleById(uid);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        DataView dataView = new DataView();
        if (StringUtils.equals(roleName,"学生") ){
            dataView.setCode(100);
            dataView.setMsg(roleName+"角色不能进行编辑！");
            return dataView;
        }

        boolean b =healthClockService.updateById(healthClock);
        if (b){
            dataView.setCode(200);
            dataView.setMsg("修改成功！");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("修改失败！");
        return dataView;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteHealthClockById")
    @ResponseBody
    public DataView deleteHealthClockById(Integer id, HttpSession session){
        //根据角色判断是否能编辑
        // 1.取到
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<Integer> role = roleService.queryUserRoleById(uid);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        DataView dataView = new DataView();
        if (StringUtils.equals(roleName,"学生") ){
            dataView.setCode(100);
            dataView.setMsg(roleName+"角色不能进行删除！");
            return dataView;
        }


        healthClockService.removeById(id);
        dataView.setCode(200);
        dataView.setMsg("删除数据成功！");
        return dataView;
    }

}
